package models;

import play.mvc.*;
import play.libs.*;
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;
import akka.actor.*;
import static akka.pattern.Patterns.ask;

import com.fasterxml.jackson.databind.JsonNode;

import java.util.*;

import models.msgs.*;
import models.msgs.Number;
import static java.util.concurrent.TimeUnit.*;

public class Table extends UntypedActor {
	
	
	public Creator creator = new Creator();

	//Flaga zapobiegająca wielokrotnemu rozdaniu kart
	private boolean rozdane_karty=false;
	//mozliwe ze nie bedzie potrzebne
	private int licznik_wejsc = 1;
	//Ranking wolnych miejsc przy stole
	private static int[] wolne_miejsce = new int[4];
	
	
    // Default table.
    static ActorRef defaultTable = Akka.system().actorOf(Props.create(Table.class));
    
    // Members of this table.
    static Map<String, ActorRef> members = new HashMap<String, ActorRef>();
    
    private void GiveCards(int gracze){
		 int i,j;
		 
		 for(j=0;j<5;j++){
			 for(i=0;i<gracze;i++){
				 creator.hand[i].cards[j]=creator.deck.GetCard();
			 }
		 }
		 for(i=0;i<gracze;i++){
			 creator.hand[i].sort();
		 }
	 }
       
    public static void join(final String name, WebSocket.In<JsonNode> in, WebSocket.Out<JsonNode> out) throws Exception{
    	int t;
        // Send the Join message to the table
        String result = (String)Await.result(ask(defaultTable,new Join(name), 1000), Duration.create(1, SECONDS));
                
        if("OK".equals(result)) {
        	
        for(t=0;t<4;t++)
        		if(wolne_miejsce[t]==0){
        			wolne_miejsce[t]=1;
        			break;
        		}
            ActorRef player =  Akka.system().actorOf(
                    Props.create(Human.class, name, in, out,  defaultTable , t));
            
            members.put(name, player);            
            String text =  "my Actor has been added to Table"; 
            notifyAll(new Info(text,name));           
        }
    }
    
    public static void quit(final String username) throws Exception{
    	
        //to do
    }
    
       
    public void onReceive(Object message) throws Exception {
        if(message instanceof Join) {
            
            //Join join = (Join) message;                                
            getSender().tell("OK", getSelf());           
        } 
        else if (message instanceof Number)
        {   
        	
        	String nr = ((Number) message).getCode();
        	//String name = ((Number) message).getName();  
        	//getSender().tell(Deck.giveCard(nr), getSelf()); narazie ne potym
        	
        	//String text = "Karty gracza "+nr+" to:";
        	//notifyAll(new Info(text,"Table"));
        	//for(int j=0; j<5; j++){
	        //text = creator.hand[nr].cards[j].getRank()+"   "+creator.hand[nr].cards[j].getColor();
	        //notifyAll(new Info(text,"Table"));
        	//}
        	
        }
        else if (message instanceof Info)
        {   
        	String text = ((Info) message).getText();
        	String name = ((Info) message).getName();  
        	
        	notifyAll(new Info(text, name));
        }  
        else if(message instanceof Quit)  {            
           
            String name = ((Quit)message).getName(); 
            int nr = ((Quit)message).getNumber(); 
            wolne_miejsce[nr]=0;
            members.remove(name);
            String text = "has left the game";            		
            notifyAll(new Info(text, name));
          
        //rozdane_karty zapobiegają wielokrotnemu rozdaniu kart
        } else if(message instanceof Rozdaj)  {  
        	String text = "";
        	if(members.size() < 2)//doz miany<-------------------------------------------------------------
        	{
	        	text = "Prosze czekać na pozostałych graczy";   
	            getSender().tell(new Info(text, "Table"), getSelf());
        	} 
        	else if(!rozdane_karty) 
        	{
        		rozdane_karty=true;
	        	GiveCards(members.size());
	        	notifyAll(new Rozdaj());
            } 
        	else if(rozdane_karty) 
            {
        		int nr = ((Rozdaj) message).getNumber();
        		licznik_wejsc++;
        		text = "Twoje karty to:";
        		getSender().tell(new Info(text, "Table"), getSelf());
            	for(int j=0; j<5; j++){
            		text = "		"+creator.hand[nr].cards[j].getRank()+"   "+creator.hand[nr].cards[j].getColor();
            		getSender().tell(new Info(text, "Table"), getSelf());
            	}
            	if(licznik_wejsc>members.size()){
            		licznik_wejsc=1;
            		rozdane_karty=!rozdane_karty;
            	}
            }
        }
        
    }
    
        
    static public void notifyAll(Object msg)
    {    	
        for (ActorRef member : members.values())
        {
            member.tell(msg, defaultTable);
        }
    }
    
}

